********************************************************************
* this code is based on the original Science paper's replication code 
* the major extension is to include battleground states indicators 
********************************************************************

clear
set more off
set segmentsize 1g

use "./data/rawdata_safegraph/DataFile_2016_HomeAndThanksgiving.dta",clear 

**************************
* Generate key variables *
**************************

* Here we generate filters to condition on being home, eating Thanksgiving not at home, then returning home
* Note: The variable Hm_TG_Close, asks that a person's estimated home location and TG location be spatialy discernable given GPS noise
* Android and IOS both report that estimated accuracy is 1 sigma, i.e. the prob that the true point is within that distance is 68%
* see: https://developer.android.com/reference/android/location/Location.html#getAccuracy%28%29 and https://stackoverflow.com/questions/30673627/location-accuracy-defined-ios/31129449#31129449
* Hm_TG_Close takes a 2 sigma error of the TG point from home, which implies a slight greater than 99% confidence

gen H_H = Was_Home_Early & Was_Home_Late
gen H_TR_H = Was_Home_Early & Was_Home_Late & Thanksgiving_Not_Home & Hm_TG_Close

gen TwoPartyShare_Home = home_clinton / (home_clinton + home_trump)
gen TwoPartyShare_TG_2016 = travel_clinton / (travel_clinton + travel_trump)

* Generate the Probability of eating with a person who voted differently *
gen double P_h = TwoPartyShare_Home
gen double P_t = TwoPartyShare_TG_2016

gen double P_Diff = P_h * (1 - P_t) + (1 - P_h) * P_t

gen double P_More_Cons = P_h * (1 - P_t)
gen double P_More_Lib = (1 - P_h) * P_t

* Generate interaction variables *
gen Pol_Ads = total_ads / 1000
gen Pol_Ads_TG = travel_total_ads / 1000

gen P_Diff_X_Pol_Ads = P_Diff * Pol_Ads
gen P_More_Cons_X_Pol_Ads = P_More_Cons * Pol_Ads
gen P_More_Lib_X_Pol_Ads = P_More_Lib * Pol_Ads

* Generate fixed effects at different spatial levels *
gen County_Home = substr(tract_fips,1,5)
gen County_TG = substr(travel_tract_fips,1,5)

egen County_Pairs = group(County_Home County_TG)
egen Zip_Pairs = group(Zip_Home Zip_Thanksgiving)
egen Geo5_Pairs = group(Geohash_Home_5 Geohash_Thanksgiving_5)

* Create a precinct-level cluster variable, since main RHS variable is measured at the precinct level *
egen PrecXPrec = group(prec_code travel_prec_code)

*********************************************************************************************************************************************
* additional variables
*********************************************************

* local-level close election measure 
gen diff_h = abs(home_clinton-home_trump) 
gen diff_tg = abs(travel_clinton-travel_trump) 

gen pbg_home = diff_h < 0.05 & ~missing(diff_h)
gen pbg_tg = diff_tg < 0.05 & ~missing(diff_tg)

* create close-election state indicators 
gen State_Home = substr(tract_fips,1,2)
	replace State_Home = "" if State_Home =="NA"
	gen st_home = real(State_Home)

gen State_TG = substr(travel_tract_fips,1,2)
	replace State_TG = "" if State_TG =="NA"
	gen st_tg = real(State_TG)

* check whether two families in the same state
gen st_same = st_home == st_tg if ~missing(st_home) & ~missing(st_tg)

tab st_same if H_TR_H 

* create close-election state indicators
gen bg_home = 0 if ~missing(st_home)
gen bg_tg= 0 if ~missing(st_tg)

* battle ground state in 2016 
foreach x of numlist  4 12 23 26 27 32 33 37 42 55 {
	replace bg_home = 1 if st_home == `x'
	replace bg_tg = 1 if st_tg == `x'
}


label var P_Diff "Probability of political mismatch"
label var Time_At_Thanksgiving "Duration of Thanksgiving dinner"
label var bg_tg "Close election state"
label var pbg_tg "Close election precinct"

* standardized political ads 
qui sum Pol_Ads 
gen std_Pol_Ads = (Pol_Ads - `r(mean)') / `r(sd)'

* create thanksgiving home indicator
gen Thanksgiving_Home = 1 - Thanksgiving_Not_Home

label var std_Pol_Ads "Standardized number of political ads"

keep ID_11_16 prec_code travel_prec_code /*
*/ Time_At_Thanksgiving Thanksgiving_Home Thanksgiving_Not_Home P_Diff std_Pol_Ads pbg_tg bg_tg bg_home /*
*/ Was_Home_Early H_TR_H PrecXPrec County_Pairs st_same /*
*/ block_white block_black block_hispanic block_asian bg_male bg_unemployed bg_avgcommutetime  /*
*/ tract_foreignbor tract_medianage tract_urbanarea tract_rural tract_medhhinc

compress
save "./data/processed/thanksgiving2016_processed.dta", replace



